package com.hontech.libratest.activity

import android.Manifest
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.navigation.NavigationBarView
import com.hontech.libratest.R
import com.hontech.libratest.activity.fragment.*
import com.hontech.libratest.utils.*
import kotlinx.coroutines.launch

class MainActivity : BaseActivity() {

    private val mViewPager: ViewPager2 by lazy { findViewById(R.id.id_main_vp) }
    private val mNav: BottomNavigationView by lazy { findViewById(R.id.id_main_bnv) }
    private val mFragments =
        arrayOf(SettingFragment(), StatusFragment(), MainFragment(), DeliveryFragment(), RkcFragment())


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        init()

        appScope.launch {

            try {
                var ret = requestPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                if (!ret) {
                    showToast("无法进行数据更新")
                }
                ret = requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                if (!ret) {
                    showToast("无法进行数据更新")
                }
                if (!canExternalStore()) {
                    val intent = Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION)
                    startActivity(intent)
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    private fun init() {
        mViewPager.adapter = Adapter(this, mFragments)
        mViewPager.registerOnPageChangeCallback(mPageChange)
        mNav.setOnItemSelectedListener(mItemSelected)
        mViewPager.offscreenPageLimit = mFragments.size
    }

    private val mPageChange = object : ViewPager2.OnPageChangeCallback() {

        override fun onPageSelected(position: Int) {
            mNav.selectedItemId = mNav.menu.getItem(position).itemId
        }
    }

    private val mItemSelected =
        NavigationBarView.OnItemSelectedListener { item ->
            when (item.itemId) {
                R.id.id_menu_setting -> mViewPager.currentItem = 0
                R.id.id_menu_status -> mViewPager.currentItem = 1
                R.id.id_menu_main -> mViewPager.currentItem = 2
                R.id.id_menu_delivery -> mViewPager.currentItem = 3
                R.id.id_menu_rkc -> mViewPager.currentItem = 4
            }
            true
        }

    private class Adapter(act: AppCompatActivity, val fragments: Array<Fragment>) :
        FragmentStateAdapter(act) {

        override fun getItemCount(): Int {
            return fragments.size
        }

        override fun createFragment(position: Int): Fragment {
            return fragments[position]
        }
    }
}

